package com.itheima.service.impl;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.ESManagerMapper2;
import com.itheima.service.ESManagerService;
import com.itheima.dao.BlogSortMapper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.TagMapper;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogInfo;
import com.itheima.service.BlogService;
import com.itheima.utils.IdWorker;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private BlogService blogService;

    @Autowired
    private ESManagerMapper2 esManagerMapper2;

    /**
     *添加博客
     * @param uid
     * @return
     */
    @Override
    public void addElasticSearchIndexByUid(String uid) {
        //获取blog对象
        Blog blog = blogService.findBlogByUid(uid);
        //将blog转换程blogInfo
        BlogInfo blogInfo = JSON.parseObject(JSON.toJSONString(blog), BlogInfo.class);


        blogInfo.setId(Long.parseLong(blog.getUid()));

        //根据sortUid获取blogSortName
        String sortName = blogSortMapper.getSortNameByUid(blog.getBlogSortUid());
        blogInfo.setBlogSortName(sortName);

        //根据fileUid获取photoUrl
        String picUrl = fileMapper.getPicUrlByUid(blog.getFileUid());
        blogInfo.setPhotoUrl(picUrl);

        //根据tagUid查询tagUidList和tagNameList
        List<String> tagUidList=new ArrayList<>();
        tagUidList.add(blog.getTagUid());
        blogInfo.setTagUidList(JSON.toJSONString(tagUidList));

        List<String> tagNameList=new ArrayList<>();
        //通过uid查询tagName
        String content = tagMapper.getContentByUid(blog.getTagUid());
        tagNameList.add(content);
        blogInfo.setTagNameList(JSON.toJSONString(tagNameList));

        //添加es
        esManagerMapper2.save(blogInfo);
    }

    /**
     *通过uid删除博客
     * @param uid
     * @return
     */
    @Override
    public void deleteElasticSearchByUid(String uid) {
        esManagerMapper2.deleteById(Long.parseLong(uid));
    }

    /**
     *通过ElasticSearch搜索博客
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo elasticSearchBlog(String keywords, Integer currentPage, Integer pageSize) {
        //构建查询条件封装对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        //按照关键字查询  模糊查询
        SearchQuery searchQuery = nativeSearchQueryBuilder.withQuery(QueryBuilders.matchPhraseQuery("summary", keywords)).build();

        //查询
        List<BlogInfo> blogInfos = elasticsearchTemplate.queryForList(searchQuery, BlogInfo.class);
        PageHelper.startPage(currentPage,pageSize);
        PageInfo pageInfo=new PageInfo(blogInfos);

        return pageInfo;
    }

    /**
     * 创建索引库结构
     */
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(BlogInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(BlogInfo.class);
    }

    @Autowired
    private BlogSortMapper blogSortMapper;

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 导入全部博客集合进入到索引库
     */
    @Override
    public void importAll() {
        //查询已上架blog集合
        List<Blog> blogList = blogService.findList();
        if (blogList == null || blogList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }

        //将json转换为blogInfo
        List<BlogInfo> blogInfoList = new ArrayList<>();
        //封装blogInfo数据
        for (Blog blog : blogList) {
            BlogInfo blogInfo = JSON.parseObject(JSON.toJSONString(blog), BlogInfo.class);

            blogInfo.setId(idWorker.nextId());
            //根据sortUid获取blogSortName
            String sortName = blogSortMapper.getSortNameByUid(blog.getBlogSortUid());
            blogInfo.setBlogSortName(sortName);

            //根据fileUid获取photoUrl
            String picUrl = fileMapper.getPicUrlByUid(blog.getFileUid());
            blogInfo.setPhotoUrl(picUrl);

            //根据tagUid查询tagUidList和tagNameList
            List<String> tagUidList=new ArrayList<>();
            tagUidList.add(blog.getTagUid());
            blogInfo.setTagUidList(JSON.toJSONString(tagUidList));

            List<String> tagNameList=new ArrayList<>();
            //通过uid查询tagName
            String content = tagMapper.getContentByUid(blog.getTagUid());
            tagNameList.add(content);
            blogInfo.setTagNameList(JSON.toJSONString(tagNameList));

            //放入集合
            blogInfoList.add(blogInfo);
        }

        //导入索引库
        esManagerMapper2.saveAll(blogInfoList);
    }

}

